package com.chuangjiangx.member.manager.web.web.stored.controller;

import com.chuangjiangx.commons.CJBeanUtils;
import com.chuangjiangx.commons.DateUtils;
import com.chuangjiangx.commons.PageUtils;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.commons.request.Page;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.manager.basic.web.controller.BaseController;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.RequestHeadersUtil;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.manager.web.web.stored.request.*;
import com.chuangjiangx.member.manager.web.web.stored.response.*;
import com.chuangjiangx.member.stored.ddd.dal.condition.*;
import com.chuangjiangx.member.stored.ddd.query.MbrRechargeRuleCountQuery;
import com.chuangjiangx.member.stored.ddd.query.dto.*;
import com.sun.jmx.snmp.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 会员储值分析
 *
 * @author 李孝延
 * @Date 2017/12/19 1:08
 **/
@RestController
@RequestMapping(value = "/api/mbr/storedrules-count", produces = "application/json")
public class MbrRechargeRuleCountController extends BaseController {

    @Autowired
    private MbrRechargeRuleCountQuery mbrRechargeRuleCountQuery;


    /**
     * 会员储值分析-按储值规则-图表-商户权限
     *
     * @map dataList
     * @see RechargeRuleCountChartForMerchantResponse
     */
    @RequestMapping(value = "/count-by-storedrules-chart-all")
    @Login
    @Deprecated
    public Response getStoredRulesCountChartForMerchant(@Validated @RequestBody RechargeRuleCountChartForMerchantRequest param) {
        RechargeRuleCountChartCondition condition = new RechargeRuleCountChartCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());

        List<RechargeRuleCountChartDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountChartForMerchant(condition);
        List<RechargeRuleCountChartForMerchantResponse> list = CJBeanUtils.convertCollection(listDto, RechargeRuleCountChartForMerchantResponse.class);
        return ResponseUtils.success("dataList", list);
    }


    /**
     * 会员储值分析-按储值规则-图表-门店权限
     *
     * @map dataList
     * @see RechargeRuleCountChartForStoreResponse
     */
    @RequestMapping(value = "/count-by-storedrules-chart-its")
    @Login
    public Response getStoredRulesCountChartForStore(HttpSession session, @Validated @RequestBody RechargeRuleCountChartForStoreRequest param) {
        RechargeRuleCountChartCondition condition = new RechargeRuleCountChartCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());

        List<RechargeRuleCountChartDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountChartForStore(condition);
        List<RechargeRuleCountChartForStoreResponse> list = CJBeanUtils.convertCollection(listDto, RechargeRuleCountChartForStoreResponse.class);
        return ResponseUtils.success("dataList", list);
    }

    /**
     * 会员储值分析-按储值规则-列表-商户权限
     *
     * @map dataList
     * @see RechargeRuleCountListForMerchantResponse
     */
    @RequestMapping(value = "/count-by-storedrules-list-all")
    @Login
    public Response getStoredRulesCountListForMerchant(@Validated @RequestBody RechargeRuleCountListForMerchantRequest param) {
        RechargeRuleCountListCondition condition = new RechargeRuleCountListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleCountListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountListForMerchant(condition);
        List<RechargeRuleCountListForMerchantResponse> list = CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleCountListForMerchantResponse.class);
        return ResponseUtils.successPage(param.getPage(), listDto, "dataList", list);
    }


    /**
     * 会员储值分析-按储值规则-列表-商户权限 导出
     */
    @RequestMapping(value = "/export/count-by-storedrules-list-all", produces = "application/json")
    @Login
    public void exportStoredRulesCountListForMerchant(HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException, ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String pageNo = request.getParameter("pageNO");
        RechargeRuleCountListForMerchantRequest param = new RechargeRuleCountListForMerchantRequest();
        if (!"".equals(startTime)) {
            param.setStartTime(sdf.parse(startTime));
        }
        if (!"".equals(endTime) ) {
            param.setEndTime(sdf.parse(endTime));
        }
        Page page = new Page();
        if (!"".equals(pageNo)) {
            page.setPageNO(Integer.parseInt(pageNo));
        }
        page.setEveryPageCount(EXPORT_SIZE);
        param.setPage(page);

        OutputStream out = response.getOutputStream();
        RequestHeadersUtil.exportRequestHeader(response, "会员储值分析按储值规则列表");

        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            List<RechargeRuleCountListForMerchantResponse> list = exportStoredRulesCountListForMerchant(param);
            osw = new OutputStreamWriter(out, "GBK");
            bw = new BufferedWriter(osw);
            bw.append("序号").append(",")
                    .append("储值规则").append(",")
                    .append("储值总额").append(",")
                    .append("储值笔数").append(",")
                    .append("\r");
            if (list != null && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    RechargeRuleCountListForMerchantResponse data = list.get(i);
                    bw.append(String.valueOf(i + 1)).append(",")
                            .append(data.getStoredRulesName()).append(",")
                            .append(data.getAmount().toString()).append(",")
                            .append(data.getCount().toString()).append(",")
                            .append("\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                bw.close();
            }
            if (osw != null) {
                osw.close();
            }
            if (out != null) {
                out.close();
            }
        }


    }

    private List<RechargeRuleCountListForMerchantResponse> exportStoredRulesCountListForMerchant(RechargeRuleCountListForMerchantRequest param) {
        RechargeRuleCountListCondition condition = new RechargeRuleCountListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleCountListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountListForMerchant(condition);
        return CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleCountListForMerchantResponse.class);
    }


    /**
     * 会员储值分析-按储值规则-列表-门店权限
     *
     * @map dataList
     * @see RechargeRuleCountListForStoreResponse
     */
    @RequestMapping(value = "/count-by-storedrules-list-its")
    @Login
    public Response getStoredRulesCountListForStore(@Validated @RequestBody RechargeRuleCountListForStoreRequest param) {
        RechargeRuleCountListCondition condition = new RechargeRuleCountListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleCountListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountListForStore(condition);
        List<RechargeRuleCountListForStoreResponse> list = CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleCountListForStoreResponse.class);
        return ResponseUtils.successPage(param.getPage(), listDto, "dataList", list);
    }


    /**
     * 会员储值分析-按储值规则-列表-门店权限 导出
     */
    @RequestMapping(value = "/export/count-by-storedrules-list-its", produces = "application/json")
    @Login
    public void exportStoredRulesCountListForStore(HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String pageNo = request.getParameter("pageNO");
        RechargeRuleCountListForStoreRequest param = new RechargeRuleCountListForStoreRequest();
        if (!"".equals(startTime)) {
            param.setStartTime(sdf.parse(startTime));
        }
        if (!"".equals(endTime)) {
            param.setEndTime(sdf.parse(endTime));
        }
        Page page = new Page();
        if (!"".equals(pageNo)) {
            page.setPageNO(Integer.parseInt(pageNo));
        }
        page.setEveryPageCount(EXPORT_SIZE);
        param.setPage(page);

        OutputStream out = response.getOutputStream();
        RequestHeadersUtil.exportRequestHeader(response, "会员储值分析按储值规则列表");

        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            List<RechargeRuleCountListForStoreResponse> list = exportStoredRulesCountListForStore(param);
            osw = new OutputStreamWriter(out, "GBK");
            bw = new BufferedWriter(osw);
            bw.append("序号").append(",")
                    .append("储值规则").append(",")
                    .append("储值总额").append(",")
                    .append("储值笔数").append(",")
                    .append("\r");
            if (list != null && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    RechargeRuleCountListForStoreResponse data = list.get(i);
                    bw.append(String.valueOf(i + 1)).append(",")
                            .append(data.getStoredRulesName()).append(",")
                            .append(data.getAmount().toString()).append(",")
                            .append(data.getCount().toString()).append(",")
                            .append("\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                bw.close();
                bw = null;
            }
            if (osw != null) {
                osw.close();
                osw = null;
            }
            if (out != null) {
                out.close();
                out = null;
            }
        }


    }

    private List<RechargeRuleCountListForStoreResponse> exportStoredRulesCountListForStore(RechargeRuleCountListForStoreRequest param) {
        RechargeRuleCountListCondition condition = new RechargeRuleCountListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleCountListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountListForStore(condition);
        return CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleCountListForStoreResponse.class);
    }


    /**
     * 按照来源统计-图表-商户权限
     *
     * @map dataList
     * @see RechargeRuleTerminalCountChartForMerchantResponse
     */
    @RequestMapping(value = "/count-by-storedrules-terminal-chart-all")
    @Login
    public Response getStoredRulesTerminalCountChartForMerchant(HttpSession session, @Validated @RequestBody RechargeRuleTerminalCountChartForMerchantRequest param) {
        RechargeRuleTerminalCountChartCondition condition = new RechargeRuleTerminalCountChartCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());

        List<RechargeRuleTerminalCountChartDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountChartForMerchant(condition);
        List<RechargeRuleTerminalCountChartForMerchantResponse> list = CJBeanUtils.convertCollection(listDto, RechargeRuleTerminalCountChartForMerchantResponse.class);
        return ResponseUtils.success("dataList", list);
    }


    /**
     * 按照来源统计-图表-门店权限
     *
     * @map dataList
     * @see RechargeRuleTerminalCountChartForStoreResponse
     */
    @RequestMapping(value = "/count-by-storedrules-terminal-chart-its")
    @Login
    public Response getStoredRulesTerminalCountChartForStore(HttpSession session, @Validated @RequestBody RechargeRuleTerminalCountChartForStoreRequest param) {
        RechargeRuleTerminalCountChartCondition condition = new RechargeRuleTerminalCountChartCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());

        List<RechargeRuleTerminalCountChartDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountChartForStore(condition);
        List<RechargeRuleTerminalCountChartForStoreResponse> list = CJBeanUtils.convertCollection(listDto, RechargeRuleTerminalCountChartForStoreResponse.class);
        return ResponseUtils.success("dataList", list);
    }

    /**
     * 按照来源统计-列表-商户权限
     *
     * @map dataList
     * @see RechargeRuleTerminalCountForListForMerchantResponse
     */
    @RequestMapping(value = "/count-by-storedrules-terminal-list-all")
    @Login
    public Response getStoredRulesTerminalCountForListForMerchant(HttpSession session, @Validated @RequestBody RechargeRuleTerminalCountForListForMerchantRequest param) {
        RechargeRuleTerminalCountForListCondition condition = new RechargeRuleTerminalCountForListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleTerminalCountForListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountForListForMerchant(condition);
        List<RechargeRuleTerminalCountForListForMerchantResponse> list = CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleTerminalCountForListForMerchantResponse.class);
        return ResponseUtils.successPage(param.getPage(), listDto, "dataList", list);
    }

    /**
     * 按照来源统计-列表-商户权限 导出
     */
    @RequestMapping(value = "/export/count-by-storedrules-terminal-list-all", produces = "application/json")
    @Login
    public void exportStoredRulesTerminalCountForListForMerchant(HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String pageNo = request.getParameter("pageNO");
        RechargeRuleTerminalCountForListForMerchantRequest param = new RechargeRuleTerminalCountForListForMerchantRequest();
        if (!"".equals(startTime)) {
            param.setStartTime(sdf.parse(startTime));
        }
        if (!"".equals(startTime)) {
            param.setEndTime(sdf.parse(endTime));
        }
        Page page = new Page();
        if (!"".equals(pageNo)) {
            page.setPageNO(Integer.parseInt(pageNo));
        }
        page.setEveryPageCount(EXPORT_SIZE);
        param.setPage(page);

        OutputStream out = response.getOutputStream();
        RequestHeadersUtil.exportRequestHeader(response, "会员储值分析按来源统计列表");

        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            List<RechargeRuleTerminalCountForListForMerchantResponse> list = exportStoredRulesTerminalCountForListForMerchant(session, param);
            osw = new OutputStreamWriter(out, "GBK");
            bw = new BufferedWriter(osw);
            bw.append("序号").append(",")
                    .append("来源").append(",")
                    .append("储值总额").append(",")
                    .append("储值笔数").append(",")
                    .append("\r");
            if (list != null && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    RechargeRuleTerminalCountForListForMerchantResponse data = list.get(i);
                    bw.append(String.valueOf(i + 1)).append(",")
                            .append(data.getPayTerminalName()).append(",")
                            .append(data.getAmount().toString()).append(",")
                            .append(data.getCount().toString()).append(",")
                            .append("\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                bw.close();
                bw = null;
            }
            if (osw != null) {
                osw.close();
                osw = null;
            }
            if (out != null) {
                out.close();
                out = null;
            }
        }


    }

    private List<RechargeRuleTerminalCountForListForMerchantResponse> exportStoredRulesTerminalCountForListForMerchant(HttpSession session, RechargeRuleTerminalCountForListForMerchantRequest param) {
        RechargeRuleTerminalCountForListCondition condition = new RechargeRuleTerminalCountForListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleTerminalCountForListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountForListForMerchant(condition);
        return CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleTerminalCountForListForMerchantResponse.class);
    }


    /**
     * 按照来源统计-列表-门店权限
     *
     * @map dataList
     * @see RechargeRuleTerminalCountForListForStoreResponse
     */
    @RequestMapping(value = "/count-by-storedrules-terminal-list-its")
    @Login
    public Response getStoredRulesTerminalCountForListForStore(HttpSession session, @Validated @RequestBody RechargeRuleTerminalCountForListForStoreRequest param) {
        RechargeRuleTerminalCountForListCondition condition = new RechargeRuleTerminalCountForListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleTerminalCountForListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountForListForStore(condition);
        List<RechargeRuleTerminalCountForListForStoreResponse> list = CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleTerminalCountForListForStoreResponse.class);
        return ResponseUtils.successPage(param.getPage(), listDto, "dataList", list);
    }

    /**
     * 按照来源统计-列表-门店权限 导出
     */
    @RequestMapping(value = "/export/count-by-storedrules-terminal-list-its", produces = "application/json")
    @Login
    public void exportStoredRulesTerminalCountForListForStore(HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String pageNo = request.getParameter("pageNO");
        RechargeRuleTerminalCountForListForStoreRequest param = new RechargeRuleTerminalCountForListForStoreRequest();
        if (!"".equals(startTime)) {
            param.setStartTime(sdf.parse(startTime));
        }
        if (!"".equals(endTime)) {
            param.setEndTime(sdf.parse(endTime));
        }
        Page page = new Page();
        if (!"".equals(pageNo)) {
            page.setPageNO(Integer.parseInt(pageNo));
        }
        page.setEveryPageCount(EXPORT_SIZE);
        param.setPage(page);

        OutputStream out = response.getOutputStream();
        RequestHeadersUtil.exportRequestHeader(response, "会员储值分析按来源统计列表");

        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            List<RechargeRuleTerminalCountForListForStoreResponse> list = exportStoredRulesTerminalCountForListForStore(session, param);
            osw = new OutputStreamWriter(out, "GBK");
            bw = new BufferedWriter(osw);
            bw.append("序号").append(",")
                    .append("来源").append(",")
                    .append("储值总额").append(",")
                    .append("储值笔数").append(",")
                    .append("\r");
            if (list != null && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    RechargeRuleTerminalCountForListForStoreResponse data = list.get(i);
                    bw.append(String.valueOf(i + 1)).append(",")
                            .append(data.getPayTerminalName()).append(",")
                            .append(data.getAmount().toString()).append(",")
                            .append(data.getCount().toString()).append(",")
                            .append("\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                bw.close();
                bw = null;
            }
            if (osw != null) {
                osw.close();
                osw = null;
            }
            if (out != null) {
                out.close();
                out = null;
            }
        }

    }

    private List<RechargeRuleTerminalCountForListForStoreResponse> exportStoredRulesTerminalCountForListForStore(HttpSession session, RechargeRuleTerminalCountForListForStoreRequest param) {
        RechargeRuleTerminalCountForListCondition condition = new RechargeRuleTerminalCountForListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleTerminalCountForListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountForListForStore(condition);
        return CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleTerminalCountForListForStoreResponse.class);
    }

    /**
     * 按储值规则查看详情-商户权限
     *
     * @map dataList
     * @see RechargeRuleCountDetailListForMerchantResponse
     */
    @RequestMapping(value = "/count-by-storedrules-detail-list-all")
    @Login
    public Response getStoredRulesCountDetailListForMerchant(HttpSession session, @Validated @RequestBody RechargeRuleCountDetailListForMerchantRequest param) {
        RechargeRuleCountDetailListCondition condition = new RechargeRuleCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());
        condition.setStoredRulesId(param.getStoredRulesId());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountDetailListForMerchant(condition);
        List<RechargeRuleCountDetailListForMerchantResponse> list = CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleCountDetailListForMerchantResponse.class);
        return ResponseUtils.successPage(param.getPage(), listDto, "dataList", list);
    }

    /**
     * 按储值规则 查询详情-商户权限
     *
     * @map dataList
     * @see RechargeRuleCountDetailListForMerchantResponse
     */
    @RequestMapping(value = "/count-by-storedrules-detail-list-search-all")
    @Login
    public Response searchStoredRulesCountDetailListForMerchant(HttpSession session, @Validated @RequestBody SearchRechargeRuleCountDetailListForMerchantRequest param) {
        RechargeRuleCountDetailListCondition condition = new RechargeRuleCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());

        condition.setMobile(param.getMobile());
        condition.setPayTerminal(param.getPayTerminal());
        condition.setStoredRulesId(param.getStoredRulesId());
        condition.setStoreName(param.getStoreName());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountDetailListForMerchant(condition);
        List<RechargeRuleCountDetailListForMerchantResponse> list = getStoredRulesCountDetailListForMerchantResponses(session, param);
        return ResponseUtils.successPage(param.getPage(), listDto, "dataList", list);
    }

    /**
     * 按储值规则 查询详情-商户权限
     */
    @RequestMapping(value = "/export/count-by-storedrules-detail-list-search-all")
    @Login
    public void exportSearchStoredRulesCountDetailListForMerchant(HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String mobile = request.getParameter("mobile");
        String payTerminal = request.getParameter("payTerminal");
        String storedRulesId = request.getParameter("storedRulesId");
        String pageNo = request.getParameter("pageNO");
        SearchRechargeRuleCountDetailListForMerchantRequest param = new SearchRechargeRuleCountDetailListForMerchantRequest();
        if (!"".equals(startTime)) {
            param.setStartTime(sdf.parse(startTime));
        }
        if (!"".equals(endTime)) {
            param.setEndTime(sdf.parse(endTime));
        }
        if (!"".equals(mobile)) {
            param.setMobile(mobile);
        }
        if (!"".equals(payTerminal)) {
            param.setPayTerminal(Byte.valueOf(payTerminal));
        }
        if (!"".equals(storedRulesId)) {
            param.setStoredRulesId(Long.parseLong(storedRulesId));
        }
        Page page = new Page();
        if (!"".equals(pageNo)) {
            page.setPageNO(Integer.parseInt(pageNo));
        }
        page.setEveryPageCount(EXPORT_SIZE);
        param.setPage(page);


        OutputStream out = response.getOutputStream();
        RequestHeadersUtil.exportRequestHeader(response, "会员储值分析按储值规则详情");


        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            List<RechargeRuleCountDetailListForMerchantResponse> list = getStoredRulesCountDetailListForMerchantResponses(session, param);
            osw = new OutputStreamWriter(out, "GBK");
            bw = new BufferedWriter(osw);
            bw.append("序号").append(",")
                    .append("交易单号").append(",")
                    .append("交易时间").append(",")
                    .append("手机号").append(",")
                    .append("储值后余额").append(",")
                    .append("支付方式").append(",")
                    .append("来源").append(",")
                    .append("\r");
            if (list != null && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    RechargeRuleCountDetailListForMerchantResponse data = list.get(i);
                    String dateTime = null;
                    if (data.getDateTime() != null) {
                        dateTime = sdf.format(data.getDateTime());
                    }
                    bw.append(String.valueOf(i + 1)).append(",")
                            .append(getCompleteExcelString(data.getStoredNum())).append(",")
                            .append(dateTime).append(",")
                            .append(data.getMobile()).append(",")
                            .append(data.getAvailableBalance().toString()).append(",")
                            .append(data.getPayEntryName()).append(",")
                            .append(data.getPayTerminalName()).append(",")
                            .append("\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                bw.close();
                bw = null;
            }
            if (osw != null) {
                osw.close();
                osw = null;
            }
            if (out != null) {
                out.close();
                out = null;
            }
        }


    }

    /**
     * 封装 按储值规则 查询详情-商户权限 方法
     */
    private List<RechargeRuleCountDetailListForMerchantResponse> getStoredRulesCountDetailListForMerchantResponses(HttpSession session, @Validated @RequestBody SearchRechargeRuleCountDetailListForMerchantRequest param) {
        RechargeRuleCountDetailListCondition condition = new RechargeRuleCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());

        condition.setMobile(param.getMobile());
        condition.setPayTerminal(param.getPayTerminal());
        condition.setStoredRulesId(param.getStoredRulesId());
        condition.setStoreName(param.getStoreName());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountDetailListForMerchant(condition);
        return CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleCountDetailListForMerchantResponse.class, (source, target) -> {
            if (target.getStoreUser() == null) {
                target.setStoreUser(user.getMerchantName());
            }
        });
    }


    /**
     * 按储值规则查看详情-门店权限
     *
     * @map dataList
     * @see RechargeRuleCountDetailListForStoreResponse
     */
    @RequestMapping(value = "/count-by-storedrules-detail-list-its")
    public Response getStoredRulesCountDetailListForStore(HttpSession session, @Validated @RequestBody RechargeRuleCountDetailListForStoreRequest param) {
        RechargeRuleCountDetailListCondition condition = new RechargeRuleCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());
        condition.setStoredRulesId(param.getStoredRulesId());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountDetailListForStore(condition);
        List<RechargeRuleCountDetailListForStoreResponse> list = CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleCountDetailListForStoreResponse.class);
        return ResponseUtils.successPage(param.getPage(), listDto, "dataList", list);
    }

    /**
     * 按储值规则 查询详情-门店权限
     *
     * @map dataList
     * @see RechargeRuleCountDetailListForStoreResponse
     */
    @RequestMapping(value = "/count-by-storedrules-detail-list-search-its")
    @Login
    public Response searchStoredRulesCountDetailListForStore(HttpSession session, @Validated @RequestBody SearchRechargeRuleCountDetailListForStoreRequest param) {
        RechargeRuleCountDetailListCondition condition = new RechargeRuleCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());
        condition.setPayTerminal(param.getPayTerminal());
        condition.setMobile(param.getMobile());
        condition.setStoredRulesId(param.getStoredRulesId());
        condition.setStoreName(param.getStoreName());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountDetailListForStore(condition);

        List<RechargeRuleCountDetailListForStoreResponse> list = CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleCountDetailListForStoreResponse.class);
        return ResponseUtils.successPage(param.getPage(), listDto, "dataList", list);
    }

    /**
     * 按储值规则 查询详情-门店权限 导出
     */
    @RequestMapping(value = "/export/count-by-storedrules-detail-list-search-its", produces = "application/json")
    @Login
    public void exportStoredRulesCountDetailListForStore(HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String mobile = request.getParameter("mobile");
        String payTerminal = request.getParameter("payTerminal");
        String storedRulesId = request.getParameter("storedRulesId");
        String pageNo = request.getParameter("pageNO");
        SearchRechargeRuleCountDetailListForStoreRequest param = new SearchRechargeRuleCountDetailListForStoreRequest();
        if (!"".equals(startTime)) {
            param.setStartTime(sdf.parse(startTime));
        }
        if (!"".equals(endTime)) {
            param.setEndTime(sdf.parse(endTime));
        }
        if (!"".equals(mobile)) {
            param.setMobile(mobile);
        }
        if (!"".equals(payTerminal)) {
            param.setPayTerminal(Byte.valueOf(payTerminal));
        }
        if (!"".equals(storedRulesId)) {
            param.setStoredRulesId(Long.parseLong(storedRulesId));
        }
        Page page = new Page();
        if (!"".equals(pageNo)) {
            page.setPageNO(Integer.parseInt(pageNo));
        }
        page.setEveryPageCount(EXPORT_SIZE);
        param.setPage(page);

        OutputStream out = response.getOutputStream();
        RequestHeadersUtil.exportRequestHeader(response, "会员储值分析按储值规则详情");

        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            List<RechargeRuleCountDetailListForStoreResponse> list = getStoredRulesCountDetailListForStoreResponses(session, param);
            osw = new OutputStreamWriter(out, "GBK");
            bw = new BufferedWriter(osw);
            bw.append("序号").append(",")
                    .append("交易单号").append(",")
                    .append("交易时间").append(",")
                    .append("手机号").append(",")
                    .append("储值后余额").append(",")
                    .append("支付方式").append(",")
                    .append("来源").append(",")
                    .append("\r");
            if (list != null && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    RechargeRuleCountDetailListForStoreResponse data = list.get(i);
                    String dateTime = null;
                    if (data.getDateTime() != null) {
                        dateTime = sdf.format(data.getDateTime());
                    }
                    bw.append(String.valueOf(i + 1)).append(",")
                            .append(getCompleteExcelString(data.getStoredNum())).append(",")
                            .append(dateTime).append(",")
                            .append(data.getMobile()).append(",")
                            .append(data.getAvailableBalance().toString()).append(",")
                            .append(data.getPayEntryName()).append(",")
                            .append(data.getPayTerminalName()).append(",")
                            .append("\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                bw.close();
                bw = null;
            }
            if (osw != null) {
                osw.close();
                osw = null;
            }
            if (out != null) {
                out.close();
                out = null;
            }
        }

    }


    /**
     * 封装 按储值规则 查询详情-门店权限 方法
     */
    private List<RechargeRuleCountDetailListForStoreResponse> getStoredRulesCountDetailListForStoreResponses(HttpSession session, @Validated @RequestBody SearchRechargeRuleCountDetailListForStoreRequest param) {
        RechargeRuleCountDetailListCondition condition = new RechargeRuleCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());
        condition.setPayTerminal(param.getPayTerminal());
        condition.setMobile(param.getMobile());
        condition.setStoredRulesId(param.getStoredRulesId());
        condition.setStoreName(param.getStoreName());
        PageUtils.copyPage(condition, param.getPage());

        PagingResult<RechargeRuleCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleCountDetailListForStore(condition);
        return CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleCountDetailListForStoreResponse.class);
    }


    /**
     * 按来源查看详情-商户权限
     *
     * @map dataList
     * @see RechargeRuleTerminalCountDetailListForMerchantResponse
     */
    @RequestMapping(value = "/count-by-storedrules-terminal-detail-list-all")
    @Login
    public Response getStoredRulesTerminalCountDetailListForMerchant(HttpSession session, @Validated @RequestBody RechargeRuleTerminalCountDetailListForMerchantRequest param) {
        List<RechargeRuleTerminalCountDetailListForMerchantResponse> list = getStoredRulesTerminalCountDetailListForMerchantResponses(session, param);
        return ResponseUtils.success("dataList", list);
    }

    private List<RechargeRuleTerminalCountDetailListForMerchantResponse> getStoredRulesTerminalCountDetailListForMerchantResponses(HttpSession session, @Validated @RequestBody RechargeRuleTerminalCountDetailListForMerchantRequest param) {
        RechargeRuleTerminalCountDetailListCondition condition = new RechargeRuleTerminalCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());
        condition.setPayTerminal(param.getPayTerminal());
        PageUtils.copyPage(condition, param.getPage());
        PagingResult<RechargeRuleTerminalCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountDetailListForMerchant(condition);
        return CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleTerminalCountDetailListForMerchantResponse.class);
    }


    /**
     * 按来源查询详情-商户权限
     *
     * @map dataList
     * @see RechargeRuleTerminalCountDetailListForMerchantResponse
     */
    @RequestMapping(value = "/count-by-storedrules-terminal-detail-list-search-all")
    @Login
    public Response searchStoredRulesTerminalCountDetailListForMerchant(@Validated @RequestBody SearchRechargeRuleTerminalCountDetailListForMerchantRequest param) {
        RechargeRuleTerminalCountDetailListCondition condition = new RechargeRuleTerminalCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());
        condition.setPayTerminal(param.getPayTerminal());
        condition.setMobile(param.getMobile());
        condition.setStoreName(param.getStoreName());

        PageUtils.copyPage(condition, param.getPage());
        PagingResult<RechargeRuleTerminalCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountDetailListForMerchant(condition);
        List<RechargeRuleTerminalCountDetailListForMerchantResponse> list = CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleTerminalCountDetailListForMerchantResponse.class, (source, target) -> {
            if (target.getStoreUser() == null) {
                target.setStoreUser(user.getMerchantName());
            }
        });
        return ResponseUtils.successPage(param.getPage(), listDto, "dataList", list);
    }


    /**
     * 按来源查询详情-商户权限
     */
    @RequestMapping(value = "/export/count-by-storedrules-terminal-detail-list-search-all", produces = "application/json")
    @Login
    public void exportStoredRulesTerminalCountDetailListForMerchant(HttpServletResponse response, HttpServletRequest request) throws IOException, ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String mobile = request.getParameter("mobile");
        String payTerminal = request.getParameter("payTerminal");
        String storeName = request.getParameter("storeName");
        String pageNo = request.getParameter("pageNO");
        SearchRechargeRuleTerminalCountDetailListForMerchantRequest param = new SearchRechargeRuleTerminalCountDetailListForMerchantRequest();
        if (!"".equals(startTime)) {
            param.setStartTime(sdf.parse(startTime));
        }
        if (!"".equals(endTime)) {
            param.setEndTime(sdf.parse(endTime));
        }
        if (!"".equals(mobile)) {
            param.setMobile(mobile);
        }
        if (!"".equals(payTerminal)) {
            param.setPayTerminal(Byte.valueOf(payTerminal));
        }
        if (!"".equals(storeName)) {
            param.setStoreName(storeName);
        }
        Page page = new Page();
        if (!"".equals(pageNo)) {
            page.setPageNO(Integer.parseInt(pageNo));
        }
        page.setEveryPageCount(EXPORT_SIZE);
        param.setPage(page);

        OutputStream out = response.getOutputStream();
        RequestHeadersUtil.exportRequestHeader(response, "会员储值分析按来源统计详情");

        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            List<RechargeRuleTerminalCountDetailListForMerchantResponse> list = exportStoredRulesTerminalCountDetailListForMerchant(param);
            osw = new OutputStreamWriter(out, "GBK");
            bw = new BufferedWriter(osw);
            bw.append("序号").append(",")
                    .append("交易单号").append(",")
                    .append("交易时间").append(",")
                    .append("手机号").append(",")
                    .append("储值规则").append(",")
                    .append("储值后余额").append(",")
                    .append("操作人").append(",")
                    .append("支付方式").append(",")
                    .append("\r");
            if (list != null && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    RechargeRuleTerminalCountDetailListForMerchantResponse data = list.get(i);
                    String dateTime = null;
                    if (data.getDateTime() != null) {
                        dateTime = sdf.format(data.getDateTime());
                    }
                    bw.append(String.valueOf(i + 1)).append(",")
                            .append(getCompleteExcelString(data.getStoredNum())).append(",")
                            .append(dateTime).append(",")
                            .append(data.getMobile()).append(",")
                            .append(data.getName())
                            .append(data.getAvailableBalance().toString()).append(",")
                            .append(data.getMobile()).append(",")
                            .append(data.getPayEntryName()).append(",")
                            .append("\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                bw.close();
                bw = null;
            }
            if (osw != null) {
                osw.close();
                osw = null;
            }
            if (out != null) {
                out.close();
                out = null;
            }
        }

    }

    private List<RechargeRuleTerminalCountDetailListForMerchantResponse> exportStoredRulesTerminalCountDetailListForMerchant(SearchRechargeRuleTerminalCountDetailListForMerchantRequest param) {
        RechargeRuleTerminalCountDetailListCondition condition = new RechargeRuleTerminalCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setMerchantId(user.getMerchantId());
        condition.setPayTerminal(param.getPayTerminal());
        condition.setMobile(param.getMobile());
        condition.setStoreName(param.getStoreName());

        PageUtils.copyPage(condition, param.getPage());
        PagingResult<RechargeRuleTerminalCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountDetailListForMerchant(condition);
        return CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleTerminalCountDetailListForMerchantResponse.class);
    }

    /**
     * 按来源查看详情-门店权限
     *
     *
     * @map dataList
     * @see RechargeRuleTerminalCountDetailListForStoreResponse
     */
    @RequestMapping(value = "/count-by-storedrules-terminal-detail-list-its")
    @Login
    public Response getStoredRulesTerminalCountDetailListForStore(@Validated @RequestBody RechargeRuleTerminalCountDetailListForStoreRequest param) {
        List<RechargeRuleTerminalCountDetailListForStoreResponse> list = getStoredRulesTerminalCountDetailListForStoreResponses(param);
        return ResponseUtils.success("dataList", list);
    }

    private List<RechargeRuleTerminalCountDetailListForStoreResponse> getStoredRulesTerminalCountDetailListForStoreResponses(@Validated @RequestBody RechargeRuleTerminalCountDetailListForStoreRequest param) {
        RechargeRuleTerminalCountDetailListCondition condition = new RechargeRuleTerminalCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());

        condition.setPayTerminal(param.getPayTerminal());
        PageUtils.copyPage(condition, param.getPage());
        PagingResult<RechargeRuleTerminalCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountDetailListForStore(condition);
        return CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleTerminalCountDetailListForStoreResponse.class, (source, target) -> {
            if (target.getStoreUser() == null) {
                target.setStoreUser(user.getMerchantName());
            }
        });
    }


    /**
     * 按来源 查询详情-门店权限
     *
     * @map dataList
     * @see RechargeRuleTerminalCountDetailListForStoreResponse
     */
    @RequestMapping(value = "/count-by-storedrules-terminal-detail-list-search-its")
    @Login
    public Response searchStoredRulesTerminalCountDetailListForStore(HttpSession session, @Validated @RequestBody SearchRechargeRuleTerminalCountDetailListForStoreRequest param) {
        RechargeRuleTerminalCountDetailListCondition condition = new RechargeRuleTerminalCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());
        condition.setStoreName(param.getStoreName());
        condition.setPayTerminal(param.getPayTerminal());
        condition.setMobile(param.getMobile());


        PageUtils.copyPage(condition, param.getPage());
        PagingResult<RechargeRuleTerminalCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountDetailListForStore(condition);
        List<RechargeRuleTerminalCountDetailListForStoreResponse> list = CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleTerminalCountDetailListForStoreResponse.class, (source, target) -> {
            if (target.getStoreUser() == null) {
                target.setStoreUser(user.getMerchantName());
            }
        });
        return ResponseUtils.successPage(param.getPage(), listDto, "dataList", list);
    }


    /**
     * 按来源 查询详情-门店权限 导出
     */
    @RequestMapping(value = "/export/count-by-storedrules-terminal-detail-list-search-its", produces = "application/json")
    @Login
    public void exportStoredRulesTerminalCountDetailListForStore(HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String mobile = request.getParameter("mobile");
        String payTerminal = request.getParameter("payTerminal");
        String storeName = request.getParameter("storeName");
        String pageNo = request.getParameter("pageNO");
        SearchRechargeRuleTerminalCountDetailListForStoreRequest param = new SearchRechargeRuleTerminalCountDetailListForStoreRequest();
        if (!"".equals(startTime)) {
            param.setStartTime(sdf.parse(startTime));
        }
        if (!"".equals(endTime)) {
            param.setEndTime(sdf.parse(endTime));
        }
        if (!"".equals(mobile)) {
            param.setMobile(mobile);
        }
        if (!"".equals(payTerminal)) {
            param.setPayTerminal(Byte.valueOf(payTerminal));
        }
        if (!"".equals(storeName)) {
            param.setStoreName(storeName);
        }
        Page page = new Page();
        if (!"".equals(pageNo)) {
            page.setPageNO(Integer.parseInt(pageNo));
        }
        page.setEveryPageCount(EXPORT_SIZE);
        param.setPage(page);

        OutputStream out = response.getOutputStream();
        RequestHeadersUtil.exportRequestHeader(response, "会员储值分析按来源统计详情");

        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            List<RechargeRuleTerminalCountDetailListForStoreResponse> list = exportStoredRulesTerminalCountDetailListForStore(session, param);
            osw = new OutputStreamWriter(out, "GBK");
            bw = new BufferedWriter(osw);
            bw.append("序号").append(",")
                    .append("交易单号").append(",")
                    .append("交易时间").append(",")
                    .append("手机号").append(",")
                    .append("储值规则").append(",")
                    .append("储值后余额").append(",")
                    .append("支付方式").append(",")
                    .append("\r");
            if (list != null && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    RechargeRuleTerminalCountDetailListForStoreResponse data = list.get(i);
                    String dateTime = null;
                    if (data.getDateTime() != null) {
                        dateTime = sdf.format(data.getDateTime());
                    }
                    bw.append(String.valueOf(i + 1)).append(",")
                            .append(getCompleteExcelString(data.getStoredNum())).append(",")
                            .append(dateTime).append(",")
                            .append(data.getMobile()).append(",")
                            .append(data.getName())
                            .append(data.getAvailableBalance().toString()).append(",")
                            .append(data.getPayEntryName()).append(",")
                            .append("\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                bw.close();
                bw = null;
            }
            if (osw != null) {
                osw.close();
                osw = null;
            }
            if (out != null) {
                out.close();
                out = null;
            }
        }

    }

    private List<RechargeRuleTerminalCountDetailListForStoreResponse> exportStoredRulesTerminalCountDetailListForStore(HttpSession session, SearchRechargeRuleTerminalCountDetailListForStoreRequest param) {
        RechargeRuleTerminalCountDetailListCondition condition = new RechargeRuleTerminalCountDetailListCondition();
        condition.setStartTime(param.getStartTime());
        condition.setEndTime(param.getEndTime() == null ? null : DateUtils.addDate(param.getEndTime()));

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        condition.setStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());
        condition.setStoreName(param.getStoreName());
        condition.setPayTerminal(param.getPayTerminal());
        condition.setMobile(param.getMobile());


        PageUtils.copyPage(condition, param.getPage());
        PagingResult<RechargeRuleTerminalCountDetailListDTO> listDto = mbrRechargeRuleCountQuery.getRechargeRuleTerminalCountDetailListForStore(condition);
        return CJBeanUtils.convertCollection(listDto.getItems(), RechargeRuleTerminalCountDetailListForStoreResponse.class);
    }


}
